package com.echo.im.platform.controller;


import com.echo.im.common.result.Result;
import com.echo.im.domain.group.vo.GroupInviteDTO;
import com.echo.im.domain.group.vo.GroupMemberVO;
import com.echo.im.domain.group.vo.GroupVO;
import com.echo.im.domain.group.dto.GroupDTO;
import com.echo.im.domain.group.dto.HandlerGroupInviteDTO;
import com.echo.im.domain.group.service.GroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author echo
 */
@Tag(name = "群聊")
@RestController
@RequestMapping("/group")
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    @Operation(summary = "创建群聊", description = "创建群聊")
    @PostMapping("/create")
    public Result<GroupDTO> createGroup(@Valid @RequestBody GroupDTO dto) {
        return Result.success(dto);
    }

    @Operation(summary = "修改群聊信息", description = "修改群聊信息")
    @PutMapping("/modify")
    public Result<GroupDTO> modifyGroup(@Valid @RequestBody GroupDTO dto) {
        return Result.success(groupService.modifyGroup(dto));
    }

    @Operation(summary = "解散群聊", description = "解散群聊")
    @DeleteMapping("/delete/{groupId}")
    public Result<Void> deleteGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        groupService.deleteGroup(groupId);
        return Result.success();
    }

    @Operation(summary = "查询群聊", description = "查询单个群聊信息")
    @GetMapping("/find/{groupId}")
    public Result<GroupVO> findGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        return Result.success(groupService.findById(groupId));
    }

    @Operation(summary = "查询群聊列表", description = "查询群聊列表")
    @GetMapping("/list")
    public Result<List<GroupVO>> findGroups() {

        return Result.success(groupService.findGroups());
    }

    @Operation(summary = "邀请进群", description = "邀请好友进群,此接口只返回前端提肯来拿表")
    @PostMapping("/askInviteTempToken")
    public Result<Map<Long,String>> askInviteTempToken(@Valid @RequestBody GroupInviteDTO vo) {
        return Result.success(groupService.invite(vo));
    }

    /**
     * 此接口必须用这个http, 因为有事务
     * @param dto
     * @return
     */
    @Operation(summary = "处理进群邀请", description = "处理进群邀请")
    @PostMapping("/handlerInvite")
    public Result<Void> handlerInvite(@Valid @RequestBody HandlerGroupInviteDTO dto) {
        groupService.handlerInvite(dto);
        return Result.success();
    }

    @Operation(summary = "查询群聊成员", description = "查询群聊成员")
    @GetMapping("/members/{groupId}")
    public Result<List<GroupMemberVO>> findGroupMembers(
            @NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        return Result.success(groupService.findGroupMembers(groupId));
    }

    @Operation(summary = "退出群聊", description = "退出群聊")
    @DeleteMapping("/quit/{groupId}")
    public Result<Void> quitGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        groupService.quitGroup(groupId);
        return Result.success();
    }

    @Operation(summary = "踢出群聊", description = "将用户踢出群聊")
    @DeleteMapping("/kick/{groupId}")
    public Result<Void> kickGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId,
                            @NotNull(message = "用户id不能为空") @RequestParam Long userId) {
        groupService.kickGroup(groupId, userId);
        return Result.success();
    }

}

